<div class="filter">
    <div class="item">搜索：</div>
    <div class="item f0">
        <select>
            <option value="uid">账号</option>
            <option value="name">真实姓名</option>
            <option value="referee">上级代理</option>
            <option value="qq">QQ</option>
            <option value="ip">登录IP</option>
        </select>
    </div>

    <div class="f1 item" style="width: 120px;">
        <input type="text" class="am-form-field" placeholder="" />
    </div>
    <div class="f5 item" style="width: 80px;">
        <select data-am-selected>
            <option value="-1" selected>全部层级</option>
        </select>
    </div>
    <div class="f7 item">
        <select data-am-selected>
            <option value="-1">全部状态</option>
            <option value="0">正常</option>
            <option value="1">冻结</option>
            <option value="2">停用</option>
        </select>
    </div>

    <div class="f8 item" style="width: 100px;">
        <input type="number" class="am-form-field" placeholder="未登录天数" />
    </div>

    <div class="f9 item" style="width: 90px;">
        <input type="text" class="am-form-field" placeholder="注册起始日期" id="start"
            data-am-datepicker="{theme:'success'}" readonly required />
    </div>
    <div class="f10 item" style="width: 90px;">
        <input type="text" class="am-form-field" placeholder="注册结束日期" id="end"
            data-am-datepicker="{theme:'success'}" readonly required />
    </div>
    <div class="f20 item" style="width: 90px;">
        <input type="text" class="am-form-field" placeholder="登录起始日期" id="loginStart"
            data-am-datepicker="{theme:'success'}" readonly required />
    </div>
    <div class="f21 item" style="width: 90px;">
        <input type="text" class="am-form-field" placeholder="登录结束日期" id="loginEnd"
            data-am-datepicker="{theme:'success'}" readonly required />
    </div>

    <div class="f22 item" style="width: 90px;">
        <input type="text" class="am-form-field" placeholder="充值始" />
    </div>
    <div class="f23 item" style="width: 90px;">
        <input type="text" class="am-form-field" placeholder="充值末" />
    </div>
    <div class="f24 item" style="width: 90px;">
        <input type="text" class="am-form-field" placeholder="取款始" />
    </div>
    <div class="f25 item" style="width: 90px;">
        <input type="text" class="am-form-field" placeholder="取款末" />
    </div>

    <div class="f21 item" style="width: 90px;">
        <select data-am-selected>
            <option value="-1">全部会员</option>
            <option value="1">关注会员</option>
        </select>
    </div>

    <div class="button item">
        <button type="button" class="am-btn am-btn-primary">查看</button>
    </div>
    <div class="clear"></div>
</div>
<div class="red" style="line-height:30px;">
    【操作提示】今日存款、取款每10分钟统计一次
</div>
<table class="am-table am-table-bordered am-table-radius am-table-striped am-table-compact" id="result-table" 
    style="min-width: 800px;">
    <thead>
    <tr>
        <th> </th>
        <th>会员</th>
        <th>上级代理</th>
        <th>会员层级</th>
        <th><i class="am-icon-sort-numeric-as hide" value='balance'></i><i class="am-icon-sort-numeric-desc" value='balanceDesc'></i> 可用额度</th>
        <th>状态</th>
        <th><i class="am-icon-sort-numeric-asc hide" value="register"></i><i class="am-icon-sort-numeric-desc active" value="registerDesc"></i>  新增时间</th>
        <th><i class="am-icon-sort-numeric-asc hide" value="login"></i><i class="am-icon-sort-numeric-desc" value="loginDesc"></i> 最后登录时间</th>
        <th>存取总数</th>
        <th><i class="am-icon-sort-numeric-asc hide" value="depositToday"></i><i class="am-icon-sort-numeric-desc" value="depositTodayDesc"></i> 今日存款</th>
        <th><i class="am-icon-sort-numeric-asc hide" value="withdrawToday"></i><i class="am-icon-sort-numeric-desc" value="withdrawTodayDesc"></i> 今日取款</th>
        <th>提现流水</th>
        <th><i class="am-icon-sort-numeric-asc hide" value="deposit"></i><i class="am-icon-sort-numeric-desc" value="depositDesc"></i> 存款总额</th>
        <th><i class="am-icon-sort-numeric-asc hide" value="withdraw"></i><i class="am-icon-sort-numeric-desc" value="withdrawDesc"></i> 取款总额</th>
        <th>未登录</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<div class="summary">
    平台所有会员剩余的总余额：<span class="red" id="all-balance-total"></span>
</div>
<div class="zxf_pagediv"></div>
<div style="height: 150px;"></div>

<style type="text/css">
    #result-table td {
        padding: 6px 2px 6px 2px;
        line-height: 20px;
        text-align: center;
    }
    #result-table td button {
        border-radius: 3px;
        padding-left: 2px;
        padding-right: 2px;
        background: #fff;
        color: #000;
    }
    #result-table td select {
        width: 50px;
    }
    #result-table i {
        cursor: pointer;
    }
    #result-table i.active {
        color: red;
    }
    i.hide {
        display: none;
    }
</style>

<script>
    $("#start").datepicker({format: 'yyyy-mm-dd'});
    $("#end").datepicker({format: 'yyyy-mm-dd'});
    $("#loginStart").datepicker({format: 'yyyy-mm-dd'});
    $("#loginEnd").datepicker({format: 'yyyy-mm-dd'});

    // 填充层级
    Layer.query(undefined, function(r) {
        var arr = Layer.sort(Layer.config);
        for (var i in arr) {
            var d = arr[i];
            $(".f5 select").append('<option value="' + d.level + '">' + d.name + '</option>');
        }
    });

    // 是不是需要列出某个代理的会员信息
    if (context.center.refereeOper == "list" && context.center.referee) {
        context.center.refereeOper = undefined;
        $(".filter .f0 select").val("referee");
        $(".f1 input").val(context.center.referee);
    }
    else if (context.center.layerOper == 'list' && context.center.layer !== undefined) {
        $(".f5 select").val(context.center.layer);
    }
    else {
        Util.restorePageFilter(function(extra) {
            var sort = extra[0];
            _setSortActive(sort, true);
        });
    }

    var _f = Util.queryTemplate($("#result-table tbody"), ".zxf_pagediv", $(".filter .button button"), function(page, size, cb) {
        var data = {
            minGrade: 0,
            maxGrade: 48,
            pageNum: page,
            pageSize: size,
        };
        var state = $(".f7 select").val();
        if (state != -1) data.state = state;
        var unknown = $(".f1 input").val().trim();
        if (unknown) {
            data[$(".f0 select").val()] = unknown;
        }

        // 注册起止日期
        if ($("#start").val()) data.minRegister = new Date($("#start").val()).getTime();
        if ($("#end").val()) data.maxRegister = new Date($("#end").val()).getTime();
        if ($("#loginStart").val()) data.minLogin = new Date($("#loginStart").val()).getTime();
        if ($("#loginEnd").val()) data.maxLogin = new Date($("#loginEnd").val()).getTime();
        
        // 未登录天数
        var v = $(".f8 input").val().trim();
        if (v) {
            var t = new Date().getTime() - v * 24 * 3600 * 1000;
            data.maxLogin = t;
        }

        var v = $(".f21 select").val();
        if (v == 1) data.tail = 1;

        var v = $(".f5 select").val();
        if (v != -1) data.layer = v;

        var v = $(".f22 input").val();
        if (v) data.minDeposit = parseInt(v) * 100;
        var v = $(".f23 input").val();
        if (v) data.maxDeposit = parseInt(v) * 100; 
        var v = $(".f24 input").val();
        if (v) data.minWithdraw = parseInt(v) * 100; 
        var v = $(".f25 input").val();
        if (v) data.maxWithdraw = parseInt(v) * 100; 

        // 排序
        var sort = $("#result-table i.active").attr("value");
        data.sort = sort;

        Util.savePageFilter([".f7 select", ".f1 input", ".f0 select", ".f8 input", 
            "#start", "#end", "#loginStart", "#loginEnd", ".f5 select", ".f22 input", ".f23 input",
            ".f24 input", ".f25 input",
        ], [sort])
        startLoading();
        CMD.get_user_list(data, page, size, function(r) {
            stopLoading();
            cb(r);
            //registerHover(r.list);
            registerOpers(r.list);
        });
    }, function(table, list, sum) {
        table = $("#result-table tbody"); table.empty();
        if (Util.isEmpty(list)) {
            table.append('<tr><td colspan="15">暂时没有数据</td></tr>');
            return;
        }
        var ips = [];
        for (var i in list) {
            var d = list[i];
            var account = {
                deposit: 0,
                withdraw: 0,
                dw: 0,
            };
            for (var j in d.account) {
                if (d.account[j].owner == "") {
                    account = d.account[j];
                    break;
                }
            }

            var tr = $("<tr value='" + d.mobile + "'></tr>");
            if (d.online == 1)
                tr.append('<td><i class="am-icon-circle" style="color:blue"></i></td>');
            else
                tr.append('<td><i class="am-icon-circle" style="color:gray"></i></td>');

            // 会员名字
            var l = '<td style="color:#000;">__tail____uid__<span style="color:#787b80">(__n__)</span></td>';
            l = l.replace(/__uid__/g, d.mobile);
            l = l.replace(/__n__/, d.name || '');
            if (d.tail == 1) {
                l = l.replace(/__tail__/, '<i class="am-icon-star red"></i> ');
            }
            else {
                l = l.replace(/__tail__/, '');
            }
            tr.append(l);

            tr.append('<td>' + (d.referee || '') + '</td>');

            // 用户层级
            var layer = Layer.config[d.layer] || {};
            if (d.layerLock == 1) {
                var l = '<td><span class="am-icon-unlock-alt layer red" value="__v__">' + (layer.name || '') + '</span></td>';
                l = l.replace(/__v__/g, d.mobile);
                tr.append(l);
            }
            else {
                var l = '<td><span class="am-icon-unlock layer" value="__v__">' + (layer.name || '') + '</span></td>';
                l = l.replace(/__v__/g, d.mobile);
                tr.append(l);
            }
                        
            tr.append("<td>" + (d.balance/100).toFixed(2) + "</td>");

            // 状态
            if (d.state == 0) tr.append("<td>正常</td>");
            else if (d.state == 1) tr.append("<td>冻结</td>");
            else if (d.state == 2) tr.append("<td>停用</td>");

            tr.append('<td>' + new Date(d.register).Format("MM/dd HH:ss") + '</td>');
            tr.append('<td>' + new Date(d.login).Format("MM/dd HH:ss") + '</td>');
            tr.append("<td>" + (d.depositCount || 0) + "/" + (d.withdrawCount || 0) + "</td>");

            tr.append("<td>" + ((d.depositToday || 0)/100) + "</td>");
            tr.append("<td>" + ((d.withdrawToday || 0)/100) + "</td>");
            tr.append("<td>" + ((d.dw || 0)/100) + "</td>");
            tr.append("<td>" + ((d.deposit || 0)/100) + "</td>");
            tr.append("<td>" + ((d.withdraw || 0)/100) + "</td>");

            // 未登录天数
            var t = (new Date().getTime() - new Date(d.login).getTime())/(1000 * 24 * 3600);
            t = parseInt(t);
            tr.append("<td>" + t + "</td>");
            
            var oper = '<button class="edit" value="__v__">修改</button>\
                <button class="add_balance" value="__v__">余额变更</button> \
                <button class="bet" value="__v__">注单明细</button> \
                <button class="balance" value="__v__">资金明细</button> \
                <select class="more" value="__v__" denyau="1"> \
                    <option value="-1" selected>更多操作</option>\
                    <option value="detail">查看详情</option>\
                    <option value="dw">打水编辑</option>\
                    <option value="lottery">盘口设定</option>\
                    <option value="delete">删除会员</option>\
                    <option value="daili">更换代理</option>\
                    <option value="layer">锁定层级</option>\
                    <option value="tail">__tail__</option>\
                </select>\
            ';
            oper = oper.replace(/__v__/g, d.mobile);
            if (d.tail == 1) {
                oper = oper.replace(/__tail__/, "取消关注");
            }
            else {
                oper = oper.replace(/__tail__/, "添加关注");
            }
            tr.append("<td>" + oper + "</td>");
            table.append(tr);

            if (d.ip && ips.indexOf(d.ip) == -1) ips.push(d.ip);
        }

        table.find("span.layer").click(function(e) {
            // 编辑分层
            var uid = $(e.currentTarget).attr("value");
            for (var i in list) {
                if (list[i].mobile == uid) {
                    context.center.user = list[i];
                    context.center.layerOper = "edit";
                    Util.addPopUp("lock_layer", "lock_layer", 320);
                    return;
                }
            }
        });
    });

    // 总余额
    CMD.account_balance(function(r) {
        $("#all-balance-total").html(r/100);
    });

    // 注册悬浮信息
    function registerHover(list) {
    }

    // 排序的操作
    $("#result-table i").click(function(e) {
        var target = $(e.currentTarget);
        var arr = target.parent().find("i");
        var sort = target.attr("value");
        if (target.attr("class").indexOf("active") != -1) {
            // 需要切换下升序、降序
            target.removeClass("active");
            target.hide();
            for (var i = 0; i < arr.length; i++) {
                if ($(arr[i]).attr("value") != target.attr("value")) {
                    $(arr[i]).show();
                    $(arr[i]).addClass("active");
                    sort = $(arr[i]).attr("value");
                }
            }
        }
        _setSortActive(sort);
    });
    function _setSortActive(sort, first) {
        $("#result-table i").removeClass("active");
        var target = $("#result-table i[value='" + sort + "']");
        target.addClass("active");
        target.show();
        var arr = target.parent().find("i");
        for (var i = 0; i < arr.length; i++) {
            if ($(arr[i]).attr("value") != sort) {
                $(arr[i]).hide();
            }
        }
        console.log("排序", sort);
        if (!first) _f();
    }

    // 注册操作项的处理
    function registerOpers(list) {
        var findUser = function(uid) {
            for (var i in list) {
                if (list[i].mobile == uid) return list[i];
            }
        };

        $("#result-table button.edit").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;

            parent._f = _f;
            context.center.user = d;
            context.center.userOper = 'edit';
            Util.addPopUp("user_info", "user_info", 390);
        });

        $("#result-table button.add_balance").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;

            context.center.user = d;
            PageHistory.load("财务管理", "余额变更", 0);
        });

        $("#result-table button.bet").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;

            context.center.user = d;
            context.center.betOper = 'seeOne';
            PageHistory.load("投注记录", "历史注单", 0);
        });

        $("#result-table button.balance").click(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;

            context.center.user = d;
            context.center.balanceOper = 'seeOne';
            PageHistory.load("财务管理", "账变记录", 0);
        });

        $("#result-table select").change(function(e) {
            var d = findUser($(e.currentTarget).attr("value"));
            if (!d) return;
            var oper = $(e.currentTarget).val();
            if (oper == -1) return;
            $(e.currentTarget).val(-1);
            if (oper == "detail") {
                context.center.user = d;
                parent._f = _f;
                Util.addPopUp("user_detail_page", "user_detail_page", 500);
            }
            else if (oper == "dw") {
                context.center.dwOper = 'seeOne';
                PageHistory.load("财务管理", "打水编辑", 0);
            }
            else if (oper == "lottery") {
                context.center.lotteryOper = 'person';
                context.center.user = d;
                PageHistory.load("游戏管理", "盘口设定", 0);
            }
            else if (oper == "delete") {
                if (confirm("确定删除会员：" + d.mobile + "吗？")) {
                    CMD.user_state(d.id, 2, function() {
                        _f();
                    });
                }
            }
            else if (oper == "daili") {
                context.center.user = d;
                parent._f = _f;
                Util.addPopUp("switch_daili", "switch_daili", 320);
            }
            else if (oper == "layer") {
                context.center.user = d;
                context.center.layerOper = "edit";
                parent._f = _f;
                Util.addPopUp("lock_layer", "lock_layer", 320);
            }
            else if (oper == "tail") {
                startLoading();
                if (d.tail == 1) {
                    CMD.user_tail(d.id, 0, function() {
                        stopLoading();
                        alert2("取消关注成功", false);
                        _f();
                    });
                }
                else {
                    CMD.user_tail(d.id, 1, function() {
                        stopLoading();
                        alert2("关注成功", false);
                        _f();
                    });
                }
            }
        });
    }
</script>
